﻿// <copyright file="EntityPropertiesTool.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>EntityPropertiesTool class</summary>

namespace BESASM.EMGAATS.UI.ArcMap
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Text;
  using BESASM.EMGAATS.BusinessClasses;
  using ESRI.ArcGIS.Carto;
  using ESRI.ArcGIS.Geodatabase;

  /// <summary>
  /// A tool for opening the properties of a selected entity feature.
  /// </summary>
  public class EntityPropertiesTool : ESRI.ArcGIS.Desktop.AddIns.Tool
  {
    /// <summary>
    /// Called by ArcMap when the tool is to be updated.
    /// </summary>
    protected override void OnUpdate()
    {
      if (EmgaatsExtension.Instance != null && EmgaatsExtension.Instance.NetworkManager != null)
      {
        this.Enabled = EmgaatsExtension.Instance.NetworkManager.IsLoaded;
      }
      else
      {
        this.Enabled = false;
      }
    }

    /// <summary>
    /// Called when the user lets the mouse button up.
    /// </summary>
    /// <param name="arg">The mouse event arguments associated with the mouse event.</param>
    protected override void OnMouseUp(MouseEventArgs arg)
    {
      FormsUtility.SafeExecute(delegate
      {
        base.OnMouseUp(arg);

        IMap map = ArcMap.Document.FocusMap;

        // TODO: Get user selected pixel tolerance from map/document properties
        IFeatureLayer entityLayer = null;
        IFeature entityFeature = FeatureLayerUtility.FindEntityAtLocation(map, arg.X, arg.Y, 4, out entityLayer);
        if (entityFeature != null)
        {
          IEntity entity = EmgaatsExtension.Instance.NetworkManager.GetEntityForObject(entityFeature);

          if (entity != null)
          {
            ((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

            map.ClearSelection();
            map.SelectFeature(entityLayer, entityFeature);

            ((IActiveView)map).PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

            switch (entity.EntityType)
            {
              case EntityType.Link:
                LinkPropertyEditor linkForm = new LinkPropertyEditor() { Link = (ILink)entity };
                linkForm.ShowDialog(FormsUtility.ApplicationWindow);
                break;
              case EntityType.Node:
                NodePropertyEditor nodeForm = new NodePropertyEditor() { Node = (INode)entity };
                nodeForm.ShowDialog(FormsUtility.ApplicationWindow);
                break;
              case EntityType.Area:
                AreaPropertyEditor areaForm = new AreaPropertyEditor() { Area = (IArea)entity };
                areaForm.ShowDialog(FormsUtility.ApplicationWindow);
                break;
              case EntityType.Director:
                DirectorPropertyEditor directorForm = new DirectorPropertyEditor() { Director = (IDirector)entity };
                directorForm.ShowDialog(FormsUtility.ApplicationWindow);
                break;
            }
          }
        }
      });
    }
  }
}
